package cn.exrick.controller;

import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.pojo.common.Result;
import cn.exrick.service.OrderReturnExchangeService;
import cn.exrick.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(description= "订单售后管理")
public class OrderReturnExchangeController {

    @Autowired
    private OrderReturnExchangeService orderReturnExchangeService;

    @RequestMapping(value = "/orderReturnExchange/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取订单售后列表")
    public DataTablesResult getList(int draw, int start, int length,Integer type, Integer status, String searchKey,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"t.order_id","m.username", "t.type", "t.reason","t.alipay_account", "t.image", "t.status","t.created"};
        String orderColumn = cols[orderCol];
        if(StringUtils.isBlank(orderColumn)) {
            orderColumn = "t.created";
        }
        if(StringUtils.isBlank(orderDir)) {
            orderDir = "desc";
        }

        if(StringUtils.isNotBlank(search)){
            searchKey = search;
        }

        return orderReturnExchangeService.getOrderReturnExchangeList(draw, start, length, type,
                status, searchKey, orderColumn, orderDir);
    }

    @RequestMapping(value = "/orderReturnExchange/deal/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "处理售后申请")
    public Result<Object> deal(@PathVariable Integer id){
        orderReturnExchangeService.deal(id);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/orderReturnExchange/refuse/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "拒绝售后申请")
    public Result<Object> refuse(@PathVariable Integer id){
        orderReturnExchangeService.refuse(id);
        return new ResultUtil<Object>().setData(null);
    }


}
